<template>
  <div class="login-container">
    <div class="login-glow"></div>
    <div class="login-form">
      <div class="login-header">
        <div class="logo-circle">
          <div class="logo-inner">
            <span class="logo-icon">🎵</span>
          </div>
        </div>
        <h1 class="app-title">圣刚钓鱼</h1>
        <p class="app-subtitle">一个灵魂安放的地方</p>

      </div>

      <form @submit.prevent="handleLogin" class="login-form-content">
        <div class="input-group">
          <input v-model="form.username" type="text" placeholder="用户名" required class="modern-input" />
        </div>

        <div class="input-group">
          <input v-model="form.password" type="password" placeholder="密码" required class="modern-input" />
        </div>

        <div class="form-options">
          <label class="checkbox-modern">
            <input v-model="form.remember" type="checkbox" class="checkbox-input" />
            <span class="checkbox-custom"></span>
            <span class="checkbox-label">记住我</span>
          </label>
          <a href="#" class="link-text">忘记密码</a>
        </div>

        <button type="submit" :disabled="loading" class="login-btn-modern">
          <span v-if="!loading">登录</span>
          <span v-else class="btn-loading">
            <span class="loading-dot"></span>
            <span class="loading-dot"></span>
            <span class="loading-dot"></span>
          </span>
        </button>
      </form>

      <div class="login-footer">
        <p class="footer-text">还没有账户？ <a href="#" @click.prevent="emit('register')" class="link-primary">立即注册</a></p>
      </div>
    </div>

    <div v-if="error" class="error-toast">
      <div class="error-content">
        <span class="error-icon">⚠️</span>
        {{ error }}
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import { useAuth } from '../composables/useAuth';

interface LoginResponse {
  data?: {
    id: string;
    username: string;
    email?: string;
    token: string;
  };
  msg?: string;
  status: boolean;
  code: number;
}

const emit = defineEmits<{
  'login-success': [user: any]
  register: []
}>()

const { login } = useAuth();

const form = ref({
  username: '',
  password: '',
  remember: false
})

const loading = ref(false)
const error = ref('')

const handleLogin = async () => {
  if (!form.value.username || !form.value.password) {
    error.value = '请填写用户名和密码'
    return
  }

  loading.value = true
  error.value = ''

  try {
    const result = await login({
      username: form.value.username,
      password: form.value.password
    });

    if (result.status) {
      emit('login-success', result.data);
    } else {
      error.value = result.msg || '登录失败';
    }
  } catch (err: any) {
    error.value = err.message || '登录失败，请稍后重试';
    console.error('Login error:', err);
  } finally {
    loading.value = false;
  }
}
</script>

<style>
/* 汽水音乐风格深色主题 */
.login-container {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  background: transparent;
  padding: 20px;
  box-sizing: border-box;
  overflow: hidden;
}

.login-glow {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 600px;
  height: 600px;
  background: radial-gradient(circle, var(--accent-primary) 0%, transparent 70%);
  border-radius: 50%;
  pointer-events: none;
  animation: pulse 4s ease-in-out infinite;
  opacity: 0.1;
}

@keyframes pulse {

  0%,
  100% {
    transform: translate(-50%, -50%) scale(1);
    opacity: 0.5;
  }

  50% {
    transform: translate(-50%, -50%) scale(1.1);
    opacity: 0.8;
  }
}

.login-form {
  background: var(--bg-card);
  backdrop-filter: blur(20px);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 30px 40px;
  width: 380px;
  max-width: 90%;
  max-height: 520px;
  box-shadow: var(--shadow-card);
  position: relative;
  z-index: 10;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.login-header {
  text-align: center;
  margin-bottom: 32px;
}

.logo-circle {
  width: 60px;
  height: 60px;
  margin: 0 auto 12px;
  background: var(--gradient-primary);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: var(--shadow-hover);
}

.logo-inner {
  width: 44px;
  height: 44px;
  background: var(--bg-tertiary);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.logo-icon {
  font-size: 24px;
  filter: grayscale(1) brightness(2);
}

.app-title {
  color: var(--text-primary);
  font-size: 24px;
  font-weight: 700;
  margin: 0 0 6px 0;
  letter-spacing: -0.3px;
}

.app-subtitle {
  color: var(--text-secondary);
  font-size: 13px;
  margin: 0;
  font-weight: 400;
}

.login-header {
  text-align: center;
  margin-bottom: 24px;
}

.input-group {
  margin-bottom: 16px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  width: 100%;
}



.modern-input {
  width: 100%;
  padding: 12px 16px;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text-primary);
  font-size: 14px;
  transition: all 0.3s ease;
  backdrop-filter: blur(10px);
  box-sizing: border-box;
}

.form-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
  width: 100%;
}



.login-footer {
  text-align: center;
  margin-top: 18px;
  padding-top: 12px;
  border-top: 1px solid rgba(255, 255, 255, 0.1);
}

.footer-text {
  color: #b3b3b3;
  font-size: 13px;
  margin: 0;
  line-height: 1.3;
}

.checkbox-custom {
  width: 18px;
  height: 18px;
  margin-right: 6px;
}

.checkbox-label {
  color: #b3b3b3;
  font-size: 13px;
}

.link-text,
.link-primary {
  color: #1db954;
  text-decoration: none;
  font-size: 13px;
  transition: color 0.3s ease;
}

.link-text:hover,
.link-primary:hover {
  color: #1ed760;
}

.login-btn-modern {
  width: 100%;
  padding: 16px;
  background: var(--gradient-primary);
  border: none;
  border-radius: 12px;
  color: var(--text-inverse);
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
  position: relative;
  overflow: hidden;
}

.login-btn-modern:hover:not(:disabled) {
  transform: translateY(-1px);
  box-shadow: 0 8px 25px rgba(29, 185, 84, 0.4);
}

.login-btn-modern:disabled {
  opacity: 0.6;
  cursor: not-allowed;
  transform: none;
}

.btn-loading {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 4px;
}

.loading-dot {
  width: 6px;
  height: 6px;
  background: var(--text-inverse);
  border-radius: 50%;
  animation: loadingDots 1.4s ease-in-out infinite;
}

.loading-dot:nth-child(2) {
  animation-delay: 0.2s;
}

.loading-dot:nth-child(3) {
  animation-delay: 0.4s;
}

@keyframes loadingDots {

  0%,
  80%,
  100% {
    opacity: 0.3;
    transform: scale(0.8);
  }

  40% {
    opacity: 1;
    transform: scale(1.2);
  }
}

.login-footer {
  text-align: center;
  margin-top: 24px;
  padding-top: 16px;
  border-top: 1px solid var(--border);
}

.footer-text {
  color: var(--text-secondary);
  font-size: 14px;
  margin: 0;
  line-height: 1.4;
}

.error-toast {
  position: fixed;
  top: 20px;
  right: 20px;
  background: var(--accent-danger);
  backdrop-filter: blur(20px);
  border-radius: 12px;
  padding: 16px 20px;
  box-shadow: var(--shadow-hover);
  animation: slideInError 0.3s ease;
  z-index: 1000;
}

.error-content {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--text-inverse);
  font-size: 14px;
  font-weight: 500;
}

@keyframes slideInError {
  from {
    transform: translateX(100%);
    opacity: 0;
  }

  to {
    transform: translateX(0);
    opacity: 1;
  }
}

@media (max-width: 480px) {
  .login-form {
    margin: 16px;
    padding: 32px 24px;
  }

  .login-container {
    padding: 16px;
  }
}
</style>